{extend name="layout/iframe" /}

{block name="content"}
<div class="row">
    <div class="col-xs-12">
        <div class="box box-success">
            <div class="box-body">
                <form action="{:url('admin/'.strtolower($model).'/setpost')}" class="form-horizontal form post-form" method="post" enctype="multipart/form-data">
                    <div class="form-body">
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        账号
                                    </label>
                                    <div class="col-md-9">
                                        <input type="text" class="form-control" id="loginName" data-required="1" name="loginName" data-name="{$info.loginName}" value="{$info.loginName}" maxlength="60">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        密码
                                        <span class="required">*</span>
                                    </label>
                                    <div class="col-md-9">
                                        <input type="password" class="form-control" id="password" data-required="1" name="password" value="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        确认密码
                                    </label>
                                    <div class="col-md-9">
                                        <input type="password" class="form-control" id="confirmpassword" data-required="1" name="confirmpassword" value="">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <input type="hidden" name="id" value="{$info.id}">
                        <input type="hidden" name="jobid" value="{$job.id}">
                        <input type="hidden" name="organizeid" value="{$organization.id}">

                        <hr />

                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="col-md-3 control-label"></label>
                                        <div class="col-md-9">
                                            <button type="button" class="btn btn-success submit-btn">确定</button>
                                            <button type="button" class="btn btn-default cancel-btn">取消</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="pagescript"}
<script>
    // 提交
    $('.submit-btn').on('click', function(){
        var name = $('#loginName').val();
        var password = $('#password').val();
        var confirmpassword = $('#confirmpassword').val();

        if(name.trim() == ''){
            layer.msg('登录名不能为空！');
            return false;
        }
        if(password.trim() == ''){
            layer.msg('密码不能为空！');
            return false;
        }
        if(password.length < 6 || password.length > 12){
            layer.msg('密码长度在6-12位之间！');
            return false;
        }
        if(password != confirmpassword){
            layer.msg('两次密码不一致！');
            return false;
        }

        var ori_name = $('#loginName').data('name');
        if(name != ori_name){
            var url = "{:url('admin/auth/checkname')}";
            $.ajax({
                url: url,
                type: 'POST',
                data: {'loginName': name},
                dataType : 'json',
                success: function (data) {
                    if(data.status == 1){
                        layer.msg('该登录名已经存在！');
                        return false;
                    }

                    $('.post-form').submit();
                    close_iframe();
                    parent.window.location.reload();
                }
            });
            return false;
        }

        $('.post-form').submit();
        close_iframe();
        parent.window.location.reload();
    });

    // 取消
    $('.cancel-btn').on('click', function(){
        close_iframe();
    });

    function close_iframe(){
        //先得到当前iframe层的索引
        var index = parent.layer.getFrameIndex(window.name);
        //再执行关闭
        parent.layer.close(index);
    }
</script>
{/block}